package com.example.floristshop;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.floristshop.database.Comment;
import com.example.floristshop.database.Flower;
import com.example.floristshop.utils.DBFindUtil;

import java.util.Date;

public class EditCommentActivity extends AppCompatActivity {

    Toolbar toolbar;
    Button submit_button;
    ImageView flower_image;
    TextView flower_name;
    EditText comment_text;

    Flower flower;
    String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_comment);

        // init data
        flower = DBFindUtil.findFlowerByFlowerId(getIntent().getLongExtra("flower_id", 1));

        // get view
        toolbar = (Toolbar)findViewById(R.id.edit_comment_toolbar);
        submit_button = (Button)findViewById(R.id.edit_comment_submit_button);
        flower_image = (ImageView)findViewById(R.id.edit_comment_flower_image);
        flower_name = (TextView)findViewById(R.id.edit_comment_flower_name);
        comment_text = (EditText) findViewById(R.id.edit_comment_text);

        // set view
        flower_image.setImageResource(flower.getImage());
        flower_name.setText(flower.getName());

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {//toolbar的返回按钮
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        submit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text = comment_text.getText().toString();
                if(text.equals("")){
                    Toast.makeText(EditCommentActivity.this, "评价内容不能为空哦", Toast.LENGTH_SHORT).show();
                }else{
                    SharedPreferences preferences = getSharedPreferences("user", MODE_PRIVATE);
                    Comment comment = new Comment((new Date().getTime())/1000L, text, flower.getFlower_id(), preferences.getLong("user_id", 10000));
                    comment.save();

                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
    }
}